<template>
  <el-form :model="model" :rules="rules" style="padding: 12px;" ref="form">
    <el-row>
      <el-col :span="24">
        <el-form-item label="选择文件：">
          <input type="file" @change="getFile($event)"/>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row :gutter="12">
      <el-col :span="12">
        <el-form-item label="版本名：" prop="name">
          <el-input v-model="model.name"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="版本号：" prop="version">
          <el-input v-model="model.version"></el-input>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24">
        <el-form-item label="备注：" prop="memo">
          <el-input type="textarea" v-model="model.memo"></el-input>
        </el-form-item>
      </el-col>
    </el-row>
  </el-form>
</template>

<script>
import request from '@/utils/request';
export default {
  name: "smart-version-create",
  data() {
    return {
      file: '',
      model: {
        name: "",
        version: "",
        memo: "",
      },
      rules: {
        name: [{ required: true, message: "请输入名称", trigger: "change" }],
        version: [
          { required: true, message: "请输入版本号", trigger: "change" },
        ],
      },
    };
  },
  methods: {
    getFile(event) {
      this.file = event.target.files[0];
    },
    upload() {
      if (!this.file || !this.file.name.includes('.smartform')) {
        this.notifyError("请选择smartform文件上传");
        return;
      }
      this.$refs['form'].validate((valid) => {
        if (valid) {
          let formData = new FormData();
          formData.append('attachmentFile', this.file);
          formData.append('name', this.model.name);
          formData.append('version', this.model.version);
          formData.append('memo', this.model.memo);

          request({
            url: '/smartform/smartVersion/publish',
            method: 'post', 
            data: formData, 
            header: { 'Content-Type': 'multipart/form-data' }
          }).then(r => {
            if (r && r.success) {
              this.notifySuccess("发布成功");
              this.$emit('done', true);
            } else {
              this.notifyError((r || {}).message || '服务器出现异常，请稍后再试');  
            }
          }).catch(e => {
            this.notifyError((e || {}).message || '服务器出现异常，请稍后再试');
          });
        }
      });
    },
  },
};
</script>

<style>
</style>